﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using SIGERP_Entidades;
using SIGERP_Negocio;

namespace SIGERP_Presentacion
{
    public partial class frmRecepcionGrafico : Form
    {
        List<Area> areasUsuario;
        Usuario u;

        public frmRecepcionGrafico()
        {
            u = Config.usuario;
            InitializeComponent();
            llenarCmbArea();    
        }

        private void llenarGrafico(object sender, EventArgs e)
        {
            DataTable dt =Reclamos.buscarCantidadPorDeptoEstado((Departamento)cmbDepto.SelectedItem);
            bool hayReclamos = false;
            foreach (DataRow fila in dt.Rows)
            {
                if (Convert.ToInt32(fila["cantidad"]) != 0) hayReclamos = true;
            }
            chart1.Series.Clear();
            var chartarea = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            chart1.ChartAreas.Clear();
            chart1.ChartAreas.Add(chartarea);
            List<Label> labels = new List<Label>();
            labels.Add(lblNPendientes);
            labels.Add(lblNEnCurso);
            labels.Add(lblNResuelto);
            foreach (DataRow fila in dt.Rows)
            {
                labels[Convert.ToInt32(fila["id_estado_reclamo"]) - 1].Text = fila["cantidad"].ToString();
            }
            btnRecepcionar.Enabled = false;
            if (hayReclamos)
            {
                btnRecepcionar.Enabled = true;
                List<string> seriesArray = new List<string>();
                List<int> pointsArray = new List<int>();
                foreach (DataRow fila in dt.Rows)
                {
                    seriesArray.Add(fila["descripcion"].ToString().Trim());
                    pointsArray.Add(Convert.ToInt32(fila["cantidad"]));
                }
                // Agregar las Series al Grafico.
                for (int i = 0; i < seriesArray.ToArray().Length; i++)
                {
                    // Aqui se agregan las series o Categorias.
                    Series series = this.chart1.Series.Add(seriesArray[i]);
                    // Aqui se agregan los Valores.
                    series.Points.Add(pointsArray[i]);

                }
                //chart1.Legends[0].Font = new Font(FontFamily.GenericSansSerif,12.0F);
                chart1.Series[0].Color = Color.Red;
                chart1.Series[1].Color = Color.Yellow;
                chart1.Series[2].Color = Color.LightGreen;
                chart1.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
            }
            }

        private void llenarCmbArea()
        {
            areasUsuario = Areas.buscarAreasPorUsuario(u);
            foreach (Area a in areasUsuario)
            {
                cmbArea.Items.Add(a);
            }
            if (cmbArea.Items.Count > 0)
                cmbArea.SelectedIndex = 0;
            cmbArea.Width = Utilities.adjustWidth(cmbArea);

        }

        private void llenarCmbDepto(object sender, EventArgs e)
        {
            List<Departamento> departamentos = Departamentos.buscarDepartamentosUsuarioArea(u,(Area)cmbArea.SelectedItem);
            cmbDepto.Items.Clear();
            if (((Area)cmbArea.SelectedItem).id != 0)
            {
                foreach (Departamento d in departamentos)
                {
                    cmbDepto.Items.Add(d);
                }
                if (cmbDepto.Items.Count > 0)
                    cmbDepto.SelectedIndex = 0;
                cmbDepto.Width = Utilities.adjustWidth(cmbDepto);
            }
        }

        private void btnRecepcionar_Click(object sender, EventArgs e)
        {
            Departamento d = (Departamento)cmbDepto.SelectedItem;
            frmRecepcionReclamoGrilla frr = new frmRecepcionReclamoGrilla(d);
            frr.FormClosed += new System.Windows.Forms.FormClosedEventHandler(cierraForm);
            frr.StartPosition = FormStartPosition.CenterParent;
            frr.ShowDialog();
            llenarGrafico(null,null);
        }

        private void cierraForm(object sender, EventArgs e)
        {
            if (this.MdiParent.GetType() == (new frmMain()).GetType())
            {
                frmMain frm = (frmMain)this.MdiParent;
                frm.mostrarCantReclamos();
            }
        }

        //private void toolStripAyuda_Click_1(object sender, EventArgs e)
        //{
        //    Help.ShowHelp(this, "/SIGERP_AYUDA.chm", HelpNavigator.Topic, "frmRecepcionGrafico.htm");
        //}
    }
}
